﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace David.WebSite.Data.Core
{
    public class PageOf<T> where T : class
    {
        public PageOf() { }

        /// <summary>
        /// 分页构造函数
        /// </summary>
        /// <param name="dataList">数据集</param>
        /// <param name="pageNo">当前页</param>
        /// <param name="pageSize">每页显示多少行</param>
        public PageOf(IQueryable<T> dataList, int pageNo, int pageSize)
        {
            if (dataList != null)
            {
                Page = pageNo;
                RowsCount = pageSize;
                TotalCount = dataList.Count();
                TotalPages = TotalCount % pageSize > 0 ? TotalCount / pageSize + 1 : TotalCount / pageSize;
                Data = dataList.Skip((pageNo - 1) * pageSize).Take(pageSize).ToList();
            }
            else
            {
                Page = pageNo;
                RowsCount = pageSize;
                TotalCount = 0;
                TotalPages = 0;
                Data = new List<T>();
            }
        }

        /// <summary>
        /// 当前页
        /// </summary>
        [DataMember]
        public int Page { get; set; }

        /// <summary>
        /// 每页显示多少行
        /// </summary>
        [DataMember]
        public int RowsCount { get; set; }

        /// <summary>
        /// 总计行数
        /// </summary>
        [DataMember]
        public int TotalCount { get; set; }

        /// <summary>
        /// 总计页数
        /// </summary>
        [DataMember]
        public int TotalPages { get; set; }

        /// <summary>
        /// 数据列表
        /// </summary>
        [DataMember]
        public IList<T> Data { get; set; }
    }
}
